package com.jgraves.WikiToSpeech;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class HistoryView extends ListActivity {

	private HistoryArrayAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		fillHistoryArrayAdapter();
	}

	private void fillHistoryArrayAdapter()
	{
		History mHistory = new History(this);
		List<String> fileArray = mHistory.selectAll();
		this.setTitle("History");
		List<HistoryOption> fileList = new ArrayList<HistoryOption>();
		for(String file: fileArray)
		{
				fileList.add(new HistoryOption(file));
		}
		Collections.sort(fileList);
		adapter = new HistoryArrayAdapter(this,R.layout.history_view,fileList);
		this.setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		// Save filename from selected list item into shared preferences
		HistoryOption selectedListItem = adapter.getItem(position);
		Intent i = new Intent();
		i.putExtra(Constants.EXTRA_FILENAME, selectedListItem.getName());
		i.putExtra(Constants.EXTRA_INPUT_TYPE,State.getDestinationType(selectedListItem.getName()));
		setResult(RESULT_OK, i);
		finish();
	}
}
